import { Component, Input, OnInit } from '@angular/core';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
declare var $;
@Component({
  selector: 'app-shop-info-integrity',
  templateUrl: './shop-info-integrity.component.html',
  styleUrls: ['./shop-info-integrity.component.scss'],
})

export class ShopInfoIntegrityComponent implements OnInit {


  @Input() tagData;
  chartOption: any; // 图表参数
  currentPercent = 90; // 默认完整程度
  circleId = 'circle';
  // multiplier
  multiplier = 2.7; // 乘数
  constructor(
    private pageModalService: PageModalService,
    private http: HttpService,
    private appGlobalService: AppGlobalService,
  ) { }

  ngOnInit() {
    this.circleId += new Date().getTime();
    let percent = 0;
    if (this.tagData.data.integrity !== undefined) {
      // tslint:disable-next-line:radix
      this.currentPercent = parseInt(this.tagData.data.integrity);
    }


    if (this.tagData.data && this.tagData.data.aiWrapCssStyle !== undefined) {
      this.multiplier = 3.6;
    }
    // tslint:disable-next-line:prefer-const
    let loading = setInterval(() => {
      if (percent > 100) {
        percent = 0;
        $('#ai_circle' + this.circleId).removeClass('clip-auto');
        $('#ai_right' + this.circleId).addClass('wth0');
      } else if (percent === this.currentPercent) {
        $('#ai_num' + this.circleId).text(percent);
        clearInterval(loading);
      } else if (percent > 50) {
        $('#ai_circle' + this.circleId).addClass('clip-auto');
        $('#ai_right' + this.circleId).removeClass('wth0');
      }
      $('#ai_left' + this.circleId).css("-webkit-transform", "rotate(" + this.multiplier * percent + "deg)");
      $('#ai_num' + this.circleId).text(percent);
      percent++;
    }, 1);
  }

  beforeSubmit(submit, link,event?) {
    if(event){
      if(typeof(event.stopPropagation())==='function'){
        event.stopPropagation();
      }
    }
    if (submit) {  // 如果有submit
      submit.sessionId = localStorage.getItem('sessionId');
      this.http.post('application/submit', submit, res => {
        // 请求成功
        if (res.success) {
          if (res.params && res.params.eventReturnParam !== undefined) {
            // tslint:disable-next-line:no-string-literal
            link['params'][res.params.eventReturnParam] = res.params[res.params.eventReturnParam];
          }
          this.pageModalService.openPage(link);
        } else {
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, 'middle');
        }
      });
    } else {
      this.pageModalService.openPage(link);
    }
  }

  goToPage(link, btn?) {
    if (btn) {
      this.beforeSubmit(btn.beforeSubmit, link);
    } else {
      if (link) {
        this.pageModalService.openPage(link);
      }
    }

  }
}

// echart-环形图 的配置：
// this.chartOption = {
    //   tooltip: {
    //     trigger: 'item',
    //     show: false
    //   },
    //   legend: {
    //     show: false
    //   },
    //   series: [
    //     {
    //       name: 'Access From',
    //       type: 'pie',
    //       radius: ['60%', '70%'],
    //       avoidLabelOverlap: false,
    //       label: {
    //         show: true,
    //         position: 'center'
    //       },
    //       emphasis: {
    //         label: {
    //           show: true,
    //           fontSize: '50',
    //           fontWeight: 'bold'
    //         }
    //       },
    //       labelLine: {
    //         show: false
    //       },
    //       data: [
    //         { value: 90, name: 'test' },
    //         { value: 10, name: 'Video Ads' }
    //       ]
    //     }
    //   ]
    // };
